package com.mg.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.mg.yygh.cmn.client.DictFeignClient;
import com.mg.yygh.hosp.repository.HospitalRepositoy;
import com.mg.yygh.hosp.service.HospitalService;
import com.mg.yygh.model.hosp.Hospital;
import com.mg.yygh.vo.hosp.HospitalQueryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author M_Dragon
 * @date 2022/8/30/030 10:25
 */
@Service
public class HospitalServiceImpl implements HospitalService {

    @Autowired
    private HospitalRepositoy hospitalRepositoy;

    @Autowired
    private DictFeignClient dictFeignClient;

    @Override
    public void save(Map<String, Object> map) {
        // 将参数map转为对象
        String jsonString = JSONObject.toJSONString(map);
        Hospital hospital = JSONObject.parseObject(jsonString, Hospital.class);
        // 查询判断对象数据是否已存在
        Hospital targetHospital = hospitalRepositoy.getHospitalByHoscode(hospital.getHoscode());
        if (targetHospital != null) {
            //已存在 更新数据，附加上原来的状态数据，时间
            hospital.setId(targetHospital.getId());
            hospital.setStatus(targetHospital.getStatus());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospital.setCreateTime(targetHospital.getCreateTime());
        } else {
            //不存在  附加上 状态数据，时间
            hospital.setStatus(0);
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospital.setCreateTime(new Date());
        }
        hospitalRepositoy.save(hospital);
    }

    @Override
    public Hospital getHospitalByCode(String hoscode) {
        Hospital hospital = hospitalRepositoy.getHospitalByHoscode(hoscode);
        return hospital;
    }

    @Override
    public Page<Hospital> getPage(Integer page, Integer limit, HospitalQueryVo hospitalQueryVo) {
        Pageable pageable = PageRequest.of(page - 1, limit);
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);
        Hospital hospital = new Hospital();
        BeanUtils.copyProperties(hospitalQueryVo, hospital);
        Example<Hospital> example = Example.of(hospital, matcher);
        Page<Hospital> hospitals = hospitalRepositoy.findAll(example, pageable);
        hospitals.getContent().stream().forEach(item -> {
            this.setDictName(item);
        });
        return hospitals;
    }

    @Override
    public void updateStatus(String id, Integer status) {
        Hospital hospital = hospitalRepositoy.findById(id).get();
        hospital.setStatus(status);
        hospital.setUpdateTime(new Date());
        hospitalRepositoy.save(hospital);
    }

    @Override
    public Map<String, Object> getHospByid(String id) {
        Hospital hospital = this.setDictName(hospitalRepositoy.findById(id).get());
        Map<String, Object> map = new HashMap<>();
        map.put("hospital", hospital);
        map.put("bookingRule", hospital.getBookingRule());
        hospital.setBookingRule(null);
        return map;
    }

    @Override
    public String getHosnameByhoscode(String hoscode) {
        Hospital hospital = getHospitalByCode(hoscode);
        if (hospital != null) {
            return hospital.getHosname();
        }
        return null;
    }

    @Override
    public List<Hospital> findByHosname(String hosname) {
        List<Hospital> hospitalList = hospitalRepositoy.findHospitalByHosnameLike(hosname);
        return hospitalList;
    }

    @Override
    public Map<String, Object> item(String hoscode) {
        HashMap<String, Object> map = new HashMap<>();
        Hospital hospital = this.setDictName(this.getHospitalByCode(hoscode));
        map.put("hospital",hospital);
        map.put("bookingRule",hospital.getBookingRule());
        hospital.setBookingRule(null);
        return map;
    }

    private Hospital setDictName(Hospital item) {
        String hostypeString = dictFeignClient.getName(item.getHostype(), "Hostype");
        String provinceString = dictFeignClient.getName(item.getProvinceCode());
        String cityString = dictFeignClient.getName(item.getCityCode());
        String districtString = dictFeignClient.getName(item.getDistrictCode());
        item.getParam().put("hostypeString", hostypeString);
        item.getParam().put("fullAddress", provinceString + cityString + districtString);
        return item;
    }
}
